import java.util.ArrayList;

public class CallExpr extends Expression
{
	private String value;
	public ArrayList<Expression> expressions;

	public DataInfo dataInfo;

	public CallExpr( String value, DataInfo dataInfo )
	{
		this.value = value;
		expressions = new ArrayList<Expression>();
		this.dataInfo = dataInfo;
	}

	public void addToList( Expression expr )
	{
		this.expressions.add( expr );
	}

	public String toString()
	{
		String result = "CallExpr " + value + "\n";
		String tbr = "";

		if( expressions.size() > 0 )
		{
			tbr = "| ProperActualParams \n";
			for( Expression ex : expressions )
			{
				tbr += "| " + ex.toString();
			}
		}

		if( tbr.length() > 0 )
			for( String s : tbr.split( "\n" ) )
			{
				result += "| " + s + "\n";
			}
		return result;
	}

	public String getType() throws SemanticException
	{

		SymbolTable st = SymbolTable.getInstance();

		if( !st.contains( value ) )
			throw new SemanticException( dataInfo.lineNumber, dataInfo.charNumber, dataInfo.line, "The method identifier " + value + " does not exist." );
		else
			return (st.get( value ).type);

	}

	public void check() throws SemanticException
	{

		SymbolTable st = SymbolTable.getInstance();

		if( st.contains( value ) )
		{

			if( !st.get( value ).classification.equalsIgnoreCase( "method" ) )
			{
				String details = "The call is not for a valid method identifier.";
				if( dataInfo != null )
					throw new SemanticException( dataInfo.lineNumber, dataInfo.charNumber, dataInfo.line, details );
				else
					throw new SemanticException( details );
			}
			Entry e = st.get( value );

			if( e.methodParameters != null )
			{

				if( e.methodParameters.length != expressions.size() )
				{
					String details = "Input parameters do not match method arguments.";
					if( dataInfo != null )
						throw new SemanticException( dataInfo.lineNumber, dataInfo.charNumber, dataInfo.line, details );
					else
						throw new SemanticException( details );
				}
				for( int i = 0; i < e.methodParameters.length; i++ )
				{

					if( !e.methodParameters[i].equalsIgnoreCase( expressions.get( i ).getType() ) )
					{
						String details = "The type of input parameters does not match that of the method arguments.";
						if( dataInfo != null )
							throw new SemanticException( dataInfo.lineNumber, dataInfo.charNumber, dataInfo.line, details );
						else
							throw new SemanticException( details );
						
					}
				}

			}

		}

	}

}
